package com.xinhuajs.articlemg.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.xinhuajs.articlemg.common.page.Page;
import com.xinhuajs.articlemg.pojo.User;
import com.xinhuajs.articlemg.service.IUserService;
import com.xinhuajs.articlemg.utils.Page_User;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private IUserService userService;
	@Autowired
	private CommonsMultipartResolver resolver;

	/*@RequestMapping(value={"/listuser"},method=RequestMethod.GET)
	public String listuser(){
		return "forward:/user/listuser.jsp";
	}
	
	@RequestMapping(value="/listuser2")
	@ResponseBody
	public Object listuser2(@RequestParam int pageSize,@RequestParam int currPage){
		Page page=new Page();
		if(pageSize!=0){
			page.setPageSize(pageSize);
		}
		if(currPage!=0){
			page.setCurrPage(currPage);
		}
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("page", page);
		userService.getUsersByPage(paramMap);
		
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("total", paramMap.get("total"));
		resultMap.put("rows", paramMap.get("rows"));
		return resultMap;
	}*/
	
	// 妞ょ敻娼扮捄瀹犳祮閺傝纭�
		@RequestMapping(value = "/userlist")
		public String userlist(Model model) {
			List<User> userlist = userService.getAll();
			model.addAttribute("userlist", userlist);
			return "forward:/WEB-INF/views/inuser/userlist.jsp";
		}

		// 濞ｈ濮炴禍鍝勬喅閺傝纭�
		@RequestMapping(value = "/addUser")
		public String addUser(HttpServletRequest request, HttpServletResponse response, @RequestParam String name,
				@RequestParam String username, @RequestParam String password, @RequestParam String unit,
				@RequestParam String job, @RequestParam String mailbox, @RequestParam String phonenum) throws Exception {
			User user = new User();
			user.setName(name);
			user.setUsername(username);
			user.setPassword(password);
			user.setUnit(unit);
			user.setJob(job);
			user.setMailbox(mailbox);
			user.setPhonenum(phonenum);
			// 濞ｈ濮為幙宥勭稊閻ㄥ嫭娲块弬鐗堟闂傦拷
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String createtime = sdf.format(date);
			user.setCreatetime(createtime);
			userService.addUser(user);
			return "list";
		}

		// 鐠哄疇娴嗘穱顔芥暭妞ょ敻娼�
		@RequestMapping(value = "/toUpUser")
		public String toUpUser(Model model, HttpServletRequest request) {
			String userid = request.getParameter("userid");
			User userlist = userService.getUserByUserid(userid);
			model.addAttribute("userlist", userlist);
			return "forward:/WEB-INF/views/inuser/upUser.jsp";
		}

		// 娣囶喗鏁兼禍鍝勬喅閺傝纭�
		@RequestMapping(value = "/upUser")
		public String upUser(HttpServletRequest request, HttpServletResponse response, @RequestParam String name,
				@RequestParam String password, @RequestParam String unit, @RequestParam String job,
				@RequestParam String mailbox, @RequestParam String phonenum, @RequestParam String userid) throws Exception {
			User user = new User();
			user.setUserid(userid);
			user.setName(name);
			user.setPassword(password);
			user.setUnit(unit);
			user.setJob(job);
			user.setMailbox(mailbox);
			user.setPhonenum(phonenum);
			// 濞ｈ濮為幙宥勭稊閻ㄥ嫭娲块弬鐗堟闂傦拷
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String createtime = sdf.format(date);
			user.setCreatetime(createtime);
			userService.upUser(user);
			return "list";
		}

		// 閹兼粎鍌ㄦ禍鍝勬喅娣団剝浼�
		@RequestMapping(value = "/selUser")
		public String selUser(HttpServletRequest request, Model model,
				@RequestParam(value = "username", required = false) String username,
				@RequestParam(value = "unit", required = false) String unit,
				@RequestParam(value = "phonenum", required = false) String phonenum,
				@RequestParam(value = "mailbox", required = false) String mailbox) {
			System.out.println("閹兼粎鍌ㄦ禍鍝勬喅娣団剝浼�==============>");
			User user = new User();
			if (request.getParameter(username) != null || request.getParameter(username) != "") {
				user.setUsername(username);
			}
			if (request.getParameter(unit) != null || request.getParameter(unit) != "") {
				user.setUnit(unit);
			}
			if (request.getParameter(phonenum) != null || request.getParameter(phonenum) != "") {
				user.setPhonenum(phonenum);
			}
			if (request.getParameter(mailbox) != null || request.getParameter(mailbox) != "") {
				user.setMailbox(mailbox);
			}
			System.out.println(user.toString());
			List<User> userlist = userService.selUser(user);
			System.out.println("閼惧嘲褰囬崚鎵畱娣団剝浼�==========" + userlist.size());
			model.addAttribute("userlist", userlist);
			return "forward:/WEB-INF/views/inuser/userlist.jsp";
		}

		@RequestMapping("/del_user")
		public String del_user(HttpServletRequest request) throws Exception {
			String userid = request.getParameter("userid");
			userService.delUser(userid);
			return "list";
		}

		@RequestMapping("/list")
		public String list(Model model, HttpServletRequest request) {

			/**
			 * 閼惧嘲褰囨い鐢告桨閸欏倹鏆�
			 */
			String page = request.getParameter("page");
			String selectRole = request.getParameter("selectRole");
			/**
			 * 閹槒顔囪ぐ鏇熸殶
			 */
			int count = 0;
			int currentPage = 0;
			Page_User pages = null;
			List<User> user = null;

			/**
			 * 閸掋倖鏌囪ぐ鎾冲妞わ拷
			 */
			if (page == null || page.equals("")) {
				currentPage = 1;
			} else {
				currentPage = Integer.parseInt(page);
			}

			/* 閼惧嘲褰囬弻銉嚄閹槒顔囪ぐ鏇熸殶 */
			count = userService.selectCount();

			/* 闁俺绻働age鏉╂瑤閲滅猾璇插讲娴犮儴骞忛崣鏍у瀻妞ょ數娈戠挧宄邦潗娑撳鐖ｉ崪灞炬蒋閺侊拷 */
			pages = new Page_User(count, currentPage);

			/* 閹峰吋甯撮崚鍡涖�夌拠顓炲綖 */
			user = userService.limit(pages.getStartIndex(), pages.getPagesize());

			model.addAttribute("pages", pages);
			model.addAttribute("userlist", user);

			return "forward:/WEB-INF/views/inuser/userlist.jsp";
		}

		@RequestMapping("/upStatus")
		public String upStatus(HttpServletRequest request) {
			User user = new User();
			String userid = request.getParameter("userid");
			user = userService.getUserByUserid(userid);
			if (user.getStatus().equals("1")) {
				user.setStatus("2");
			} else if (user.getStatus().equals("2")) {
				user.setStatus("1");
			}
			userService.upStatus(user);
			return "list";
		}
}
